package com.ecoolex.ying.space.core.manager;

import com.ecoolex.ying.space.core.dao.UserPickGoodsFavoriteDAO;
import com.ecoolex.ying.space.entity.UserPickGoodsFavorite;
import com.ecoolex.ying.space.entity.UserPickGoodsFavorite.Column;
import com.ecoolex.ying.space.entity.UserPickGoodsFavoriteExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.ying.space.entity.UserPickGoodsFavorite.IS_DELETED;
import static com.ecoolex.ying.space.entity.UserPickGoodsFavorite.NOT_DELETED;

@Service
public class UserPickGoodsFavoriteManager {
    @Autowired
    private UserPickGoodsFavoriteDAO dao;

    public boolean existByUserIdAndPickGoodsId(Integer userId, Integer pickGoodsId) {
        UserPickGoodsFavoriteExample example = new UserPickGoodsFavoriteExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andPickGoodsIdEqualTo(pickGoodsId)
                .andDeletedEqualTo(NOT_DELETED);

        return dao.countByExample(example) != 0;
    }

    public void add(Integer userId, Integer pickGoodsId, String createUser) {
        UserPickGoodsFavorite record = new UserPickGoodsFavorite();
        record.setUserId(userId);
        record.setPickGoodsId(pickGoodsId);
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    public void delete(Integer userId, Integer pickGoodsId, String updateUser) {
        UserPickGoodsFavorite record = new UserPickGoodsFavorite();
        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        UserPickGoodsFavoriteExample example = new UserPickGoodsFavoriteExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andPickGoodsIdEqualTo(pickGoodsId)
                .andDeletedEqualTo(NOT_DELETED);

        dao.updateByExampleSelective(record,
                example,
                Column.deleted,
                Column.updateUser,
                Column.updateTime);
    }
}